package com.li.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.mapper.TransferMapper;
import com.li.pojo.dto.TransferDTO;
import com.li.pojo.po.*;
import com.li.pojo.vo.TransferVo;
import com.li.service.TransferService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TransferServiceImpl implements TransferService {

    @Autowired
    private TransferMapper transferMapper;

    @Override
    public PageResult page(TransferDTO transferDTO) {
        ArrayList<TransferVo> transferVos = new ArrayList<>();
        PageHelper.startPage(transferDTO.getPageNum(),transferDTO.getPageSize());
        Page<User> transferVoPage = transferMapper.page(transferDTO.getPageNum(),transferDTO.getPageSize(),transferDTO.getUserName(),transferDTO.getPhonenumber(),transferDTO.getParams().get("beginCreateTime"),transferDTO.getParams().get("endCreateTime"));

        for (User user : transferVoPage.getResult()) {
            int clueNum = transferMapper.CluesByUser(user.getUserId());
            int businessNum = transferMapper.BusinessByUser(user.getUserId());
            TransferVo transferVo = new TransferVo();
            transferVo.setUserId(Long.valueOf(user.getUserId()));
            transferVo.setUserName(user.getUserName());
            transferVo.setCreateTime(user.getCreateTime());
            transferVo.setPhonenumber(user.getPhonenumber());
            transferVo.setBusinessNum(businessNum);
            transferVo.setClueNum(clueNum);
            transferVos.add(transferVo);
        }
        PageResult pageResult = new PageResult();
        Object params = pageResult.getParams();

        return new PageResult(200,"查询成功",params,transferVos,transferVoPage.getTotal());
    }

    @Override
    public void assignment(Integer transferUserId, String type, Integer userId) {
        log.info("{},{},{}",type,userId,transferUserId);
        List<Clue> clueList = transferMapper.ClueGetById(userId);
        List<Opportunities> opportunities = transferMapper.OppGetById(userId);
        if (type.equals("0")){
            Clue clue = clueList.get(0);
            clue.setAssignId(Long.valueOf(transferUserId));
            transferMapper.ClueAssignment(clue);
        }
        if (type.equals("1")){
            Opportunities opportunities1 = opportunities.get(0);
            opportunities1.setAssignId(Long.valueOf(transferUserId));
            transferMapper.TransferAssignment(opportunities1);
        }

    }


//    @Override
//    public List<TransferVo> page(Transfer transfer) {
//        ArrayList<Transfer> transferArrayList = new ArrayList<>();
//        ArrayList<TransferVo> transferVos = new ArrayList<>();
//        for (Transfer user : transferArrayList) {
//            TransferVo transferVo = new TransferVo();
//            int clueNum = transferMapper.CluesByUser(user.getUserId());
//            int businessNum = transferMapper.BusinessByUser(user.getUserId());
//
//            transferVo.setUserName(user.getUserName());
//            transferVo.setClueNum(clueNum);
//            transferVo.setBusinessNum(businessNum);
//            transferVo.setUserId(user.getUserId());
//            transferVo.setPhonenumber(user.getPhonenumber());
//            transferVo.setCreateTime(LocalDateTime.now().toString().replace("T", " "));
//            transferVos.add(transferVo);
//
//        }
//        return transferVos;
//    }
}
